// pages/report/report.js
const api=require('../../utils/request') 
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userHidden: true,
    nickName:'',
    avatarUrl: defaultAvatarUrl,
    phone:'',
    idNum:'',
    memberState:'',
    login:false,
    effectiveTime:'',
    isAfterShow: false,
    image:'',
    browseNum: 0,
    downloadNum:0,
    collectNum: 0
  },
  //订阅行业
  subscribe_tap(){
    wx.navigateTo({
      url: './subscribe/subscribe',
    })
  },
  //会员权益
  vip_recommend(){
    let user= wx.getStorageSync('user') 
    if(user===''){
        wx.showToast({
          title: '请登录',
          icon:'error'
        })
        return false
    }
    wx.navigateTo({
      url: '../vip/vip_recommend/vip_recommend',
    })
  },
  
 
  //跳转登录
  logout(){
    wx.clearStorage()  
     this.setData({
         login:false,
         avatarUrl: defaultAvatarUrl
     })

   // wx.clearStorage()  
    // wx.navigateTo({
    //   url: '/pages/login/login?type=1',
    // })
  },
  gzh(){
    wx.openOfficialAccountArticle({
        url:'https://mp.weixin.qq.com/s/oTo4PsPdSKFzghpisbkxew', // 此处填写公众号文章连接
        success: res => {
        },
        fail: res => {
        }
    })
  },
  set(){
    let user= wx.getStorageSync('user') 
    if(user===''){
        wx.showToast({
          title: '请登录',
          icon:'error'
        })
        return false
    }
    wx.navigateTo({
        url: './set/set',
      })
  },


  //联系客服
  kf(){
    //
   wx.openCustomerServiceChat({
       extInfo: {url: 'https://work.weixin.qq.com/kfid/kfcdb659362f1afb3c0'},
       corpId: 'wwbb8c2c0fafdbf12b',
       success(res) {
           console.log('resss',res)
       }
     })
  },
//  浏览历史
history(){
    let user= wx.getStorageSync('user') 
    if(user===''){
        wx.showToast({
          title: '请登录',
          icon:'error'
        })
        return false
    }
  wx.navigateTo({
    url: './history/history',
  })
},
downLoadPage(){
    let user= wx.getStorageSync('user') 
    if(user===''){
        wx.showToast({
          title: '请登录',
          icon:'error'
        })
        return false
    }
  wx.navigateTo({
    url: './downloadPage/downloadPage',
  })
},
collect(){
    let user= wx.getStorageSync('user') 
    if(user===''){
        wx.showToast({
          title: '请登录',
          icon:'error'
        })
        return false
    }
  wx.navigateTo({
    url: './collect/collect',
  })
},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.loadData()
    this.count()
  },

  count(){
    let user= wx.getStorageSync('user') 
    let that=this
    if(user!==''){
        let userInfo= JSON.parse(user)
        api.get('weChatMini/memberCount/'+userInfo.id).then(result=>{
            if(result.code===200){
                that.setData({
                    browseNum: result.data.browseNum,
                    downloadNum:result.data.downloadNum,
                    collectNum:result.data.collectNum

                })
            }
        })
    }
  },

  loadData(){
    let user= wx.getStorageSync('user') 
    if(user!==''){
        let that=this
        let userInfo= JSON.parse(user)
        api.get('weChatMini/getMemberById/'+userInfo.id).then(result=>{
           if(result.code===200){
               console.log('rr',result)
               let data=result.data
               if(data!==null){
                that.setData({
                    userHidden: false,
                    nickName: data.nickName,
                    avatarUrl: data.avatarUrl,
                    phone: data.phone,
                    idNum: data.idNum,
                    memberState: data.memberState,
                    memberName:data.memberName,
                    login: true,
                    effectiveTime: data.effectiveTime
                   })
               }
             
           }
        })
    }
   
  },

  onChooseAvatar(e) {
    const { avatarUrl } = e.detail
     
    let user= wx.getStorageSync('user')
    let that=this
    if(user!==''){
        let userInfo=JSON.parse(user)
        api.post('wechat/updateAvatarUrl',{userId: userInfo.id,avatarUrl:avatarUrl}).then(res=>{
           if(res.code===200){
            wx.setStorageSync('user',JSON.stringify(res.data));
            that.setData({
                avatarUrl
            })
           }else{
            wx.showToast({
                title: res.msg, // 提示的内容
                icon: 'error',   // 提示的图标，success(成功)、error(失败)、loading(加载)、none(不显示图标)
                duration: 2000,	 // 提示的延迟时间
                mask: true		 // 是否显示透明蒙层，防止触摸穿透
              })
              
           }
       })
    }else{
        api.showToast('请先登录','error')
        
    }
     
   
  },
  getPhoneNumber(e){ 
     
      let that=this
      wx.login({
        success: (res) => { 
           if(res.code){
            api.post('wechat/getUserPhone',{openIdCode:res.code,encryptedData:e.detail.encryptedData,code:e.detail.code,iv:e.detail.iv}).then(response=>{
                if(response.code===200){
                   let userInfo=response.data
                   wx.setStorageSync('user',JSON.stringify(userInfo));
                   that.loadData()
                   if(user.isShow){
                    api.get('web/researchSetRes/'+user.type).then(res=>{
                        if(res.code===200){
                           that.setData({
                            isAfterShow: true,
                            image: res.data.image
                           })
                        }else{
                            wx.showToast({
                                title:'登录失败',
                                icon: 'error',//图标，支持"success"、"loading" 
                                // image: '/images/load.gif',
                                duration: 1500,//提示的延迟时间，单位毫秒，默认：1500 
                                mask: true,//是否显示透明蒙层，防止触摸穿透，默认：false 
                              });
                        }
                     })
                   }
               }
           })
           }  
           
           
       
        },
      })

     
  },

  login(){
    wx.navigateTo({
        url: '/pages/login/login',
      })
  },

  loginAfterClose(){
      this.setData({
          isAfterShow: false
      })
  },
  about_us(){
     // 保留当前页面，跳转到应用中其他页面，不能跳转到 tabBar 页面
   wx.navigateTo({
    // 普通页面
    url:'./contact_us/contact_us'
  })
  },
  login_pc(){
       // 保留当前页面，跳转到应用中其他页面，不能跳转到 tabBar 页面
    wx.navigateTo({
      // 普通页面
      url:'./login_pc/login_pc'
    })
  },
  reading_ranking(){
    let user= wx.getStorageSync('user') 
    if(user===''){
        wx.showToast({
          title: '请登录',
          icon:'error'
        })
        return false
    }
    wx.navigateTo({
      url: './reading_ranking/reading_ranking',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.loadData()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})